// app.js
var httpUtils = require('./url.js')
App({
  onLaunch() {
    var that = this;
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
   
    // 登录
    wx.login({
      success: (res) => {
        console.log(res);
        var baseURL = httpUtils.url;
        // 将code发送到服务器后台
        wx.request({
          url: baseURL+"user/login",
          data: {
            code: res.code,
          },
          header: { "content-type": "application/json" },
          method: "GET",
          dataType: "json",
          responseType: "text",
          success: (result) => {
            console.log(result);
            var app = getApp();
            //在这里将sessionId存在本地，当做预先判断是否发起请求
            if (result.statusCode == 200) {
              wx.setStorageSync("sessionId", {
                time: Date.now(),
                data: result.data.sessionId,
              });
            }
            //获取到了openId
            app.globalData.openId = result.data.openId;
            app.globalData.uid = result.data.uid;
            console.log(app.globalData.openId);
          },
          fail: (res) => {
            console.log("获取sessionId失败！");
            console.log(res)
          },
        });
      },
      fail: () => {
        console.log("获取code失败！");
      },
    });
  },
  globalData: {
    userInfo: null,
    openId:'',
    //用于保存做题记录
    javaId:0,
    java1Id:0,
    java2Id:0,
    java3Id:0,
    java4Id:0,
    java5Id:0,
    java6Id:0,
    java7Id:0,
    java8Id:0,
    java9Id:0,
    java10Id:0,
    java11Id:0,
  }
})
